package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.entity.User;
import com.example.entity.Volunteer;
import com.example.exception.CustomException;
import com.example.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @志愿者信息业务层
 */
@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    public void add(User user) {
        //进行逻辑处理

            User dbUser = userMapper.selectByUsername(user.getUsername());
            if (ObjectUtil.isNotEmpty(dbUser)) {
                throw new CustomException("用户名已存在");
            }
            if (ObjectUtil.isEmpty(user.getPassword())) {
                user.setPassword("123456");
            }
            if(ObjectUtil.isEmpty(user.getName())){
                user.setName(user.getUsername());
            }
            user.setRole("USER");
            user.setIntegral(0);
            userMapper.insert(user);
        }

    public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> list;
        if(ObjectUtil.isNotEmpty(user.getName())){
            list = userMapper.selectByName(user.getName());
        } else {
            list = userMapper.selectAll();
        }

        return PageInfo.of(list);



    }

    public void updateById(User user) {
        userMapper.updateById(user);
    }

    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    public User login(Account account) {
        User dbUser = userMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbUser)) {
            throw new CustomException("用户不存在");
        }
        if (!account.getPassword().equals(dbUser.getPassword())) {
            throw new CustomException("账号或密码错误");
        }
        return dbUser;
    }

    public void register(Account account) {
        User user = new User();
        user.setUsername(account.getUsername());
        user.setPassword(account.getPassword());
        user.setNumber("0");
        add(user);
    }


    /**
     * 修改密码
     */
    public void updatePassword(Account account) {
        User dbUser = userMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbUser)) {
            throw new CustomException("用户不存在");
        }
        if (!account.getPassword().equals(dbUser.getPassword())) {
            throw new CustomException("原密码错误");
        }
        dbUser.setPassword(account.getNewPassword());
        userMapper.updateById(dbUser);
    }

    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    public void updatePetssId(Integer userId, Integer petssId) {
        User user = selectById(userId);
        if (user != null) {
            // 更新用户的宠物 ID
            user.setPetssId(petssId);
            updateById(user); // 调用更新用户的方法
        }
    }
}